/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package picasaphotomanager;

import com.google.gdata.client.photos.PicasawebService;
import configuration.ConfigurationManager;
import configuration.PicasaAccount;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author zalem1
 */
public class PicasaSessionManager {
    private static Map<String, PicasaSession> _sessions = new HashMap<String, PicasaSession>();
    
    public static void AddClient(PicasaAccount account)
    {       
        //Check if the client is already in the collection
        if (_sessions.containsKey(account.getPicasaName()))
            return;
        
        PicasawebService picasaService = new PicasawebService("PicasaPhotoManager");
        PicasawebClient picasaClient = new PicasawebClient(picasaService, 
                account.getLoginUsername(), account.getLoginPassword());
        
        PicasaSession newSession = new PicasaSession(account, picasaService, picasaClient);        
        _sessions.put(account.getPicasaName(), newSession);
    }
    
    public static void RemoveClient(PicasaAccount account)
    {
        if (_sessions.containsKey(account.getPicasaName()))
            _sessions.remove(account.getPicasaName());
    }
    
    public static PicasawebClient GetClient(String picasaUsername)
    {
        if (_sessions.containsKey(picasaUsername))
                return _sessions.get(picasaUsername).PicasaClient;
        
        return null;
    }

    public static PicasaSession getSession(String albumUsername)
    {
      for (PicasaAccount account : ConfigurationManager.GetPreferences().getUserAccounts())
      {
          String username = account.getLoginUsername();
          username = username.substring(0, username.indexOf('@'));
          
          if (username.equals(albumUsername))
          {
              return _sessions.get(account.getPicasaName());
          }
      }
      
      return null;
  }

    public static Set<String> GetPicasaAccounts() {
        return _sessions.keySet();
    }
    
    
}
